package org.example.controller;

import org.example.mapper.UserMapper;

import org.example.pojo.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private UserService userService;

    @GetMapping("user/info")
    @ResponseBody
    public String info(@RequestParam("id") Integer id) {
        User user = mapper.selectById(id);
        return user.toString();
    }

    @GetMapping("user/login")
    public ModelAndView login() {
        System.out.println(mapper.selectById(1) + "登录了");
        return new ModelAndView("login");
    }

    @GetMapping("user/register")
    public ModelAndView register() {
        return new ModelAndView("register");
    }

    @PostMapping("user/login")
    public ModelAndView login(@RequestParam("name") String username,
                              @RequestParam("password") String password) {
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        return userService.login(user);
    }

    @PostMapping("user/register")
    public ModelAndView register(@RequestParam("name") String username,
                                 @RequestParam("password") String password,
                                 @RequestParam("sex") Boolean sex) {
        User user = new User();
        user.setPassword(password);
        user.setSex(sex);
        user.setName(username);
        return userService.register(user);
    }
}